﻿using HDSmartCommunity.DAL;
using HDSmartCommunity.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HDSmartCommunity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        public LoginController()
        {

        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("Login")]
        public ResponseMessage Login(User user)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                UserDAL da = new UserDAL();
                User userinfo = da.GetUser(user);
                if (userinfo != null)
                {
                    message.errcode = 0;
                    message.data = userinfo;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "用户名或密码错误";
                    message.total = 0;
                }

            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "登录异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdatePwd")]
        public ResponseMessage UpdatePwd(User user)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                UserDAL da = new UserDAL();
                bool result = da.UpdatePwd(user);
                if (result)
                {
                    message.errcode = 0;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改密码失败";
                    message.total = 0;
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "登录异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 所有用户
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryUser")]
        public ResponseMessage QueryUser()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                UserDAL da = new UserDAL();
                List<User> list = da.GetAllUser();
                message.data = list;
                message.errcode = 0;
                message.total = 1;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询用户异常";
                message.total = 0;
            }
            return message;
        }
    }
}
